/**
 *  This program is free software; you can redistribute it and/or modify it under 
 *  the terms of the GNU General Public License as published by the Free Software 
 *  Foundation; either version 3 of the License, or (at your option) any later 
 *  version.
 *  You should have received a copy of the GNU General Public License along with 
 *  this program; if not, see <http://www.gnu.org/licenses/>. 
 *  Use this application at your own risk.
 *
 *  Copyright (c) 2009 by Michael Münch.
 */

package eu.micm.android.tvprogramm;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.*;
import android.view.Menu;
import android.widget.*;

public class Start extends Activity {

    public static final int MAbout = Menu.FIRST + 2;

    public static final int MBrowser = Menu.FIRST + 1;

    public void populateMenu(Menu menu) {
        MenuItem item1 = menu.add(0, MBrowser, 0, "Browser");
        MenuItem item2 = menu.add(0, MAbout, 0, "About");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        populateMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return applyMenuChoice(item) || super.onOptionsItemSelected(item);
    }

    public boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case MAbout:
            final AlertDialog about = new AlertDialog.Builder(Start.this).create();
            about.setTitle("About");
            about.setMessage(getString(R.string.about));
            about.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    about.dismiss();
                }
            });
            about.show();
            return true;
        case MBrowser:
            String surl = "http://tv.intern.de";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(surl)));
            return true;
        }
        return false;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bjetzt = (Button) findViewById(R.id.jetzt);
        bjetzt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(Start.this, TVProgramm.class);
                Bundle b= new Bundle();
                b.putString("url", "http://tv.intern.de/rss/rss.php?site=Suche&m=4&k=jetzt");
                i.putExtras(b);
                startActivity(i);
            }
        });

        Button bgleich = (Button) findViewById(R.id.gleich);
        bgleich.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(Start.this, TVProgramm.class);
                Bundle b= new Bundle();
                b.putString("url", "http://tv.intern.de/rss/rss.php?site=Suche&m=4&k=gleich");
                i.putExtras(b);
                startActivity(i);
            }
        });
        
        Button bzeit = (Button) findViewById(R.id.bzeit);
        bzeit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Spinner SpZeitAb = (Spinner) findViewById(R.id.SpZeitAb);
                Intent i = new Intent(Start.this, TVProgramm.class);
                Bundle b= new Bundle();
                String zeitab = (String) SpZeitAb.getSelectedItem();
                b.putString("url", "http://tv.intern.de/rss/rss.php?site=Suche&m=4&k=ab&start="+zeitab);
                i.putExtras(b);
                startActivity(i);
            }
        });
        Button bsuche = (Button) findViewById(R.id.bsuche);
        bsuche.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText Suche = (EditText) findViewById(R.id.Suche);
                Spinner SpZeitraum = (Spinner) findViewById(R.id.SpZeitraum);
                CheckBox CbTitle = (CheckBox) findViewById(R.id.CbTitle);
                CheckBox CbOR = (CheckBox) findViewById(R.id.CbOR);
                CheckBox CbORF = (CheckBox) findViewById(R.id.CbORF);
                Intent i = new Intent(Start.this, TVProgramm.class);
                Bundle b= new Bundle();
                String begriff = Suche.getText().toString(); ;
                String zeitraum = (String) SpZeitraum.getSelectedItem();
                zeitraum = zeitraum.substring(0,2);
                String ttl="0";
                String oe="0";
                String soe="0";
                if (CbTitle.isChecked()) {
                    ttl="1";
                }
                if (CbOR.isChecked()) {
                    oe="1";
                }
                if (CbORF.isChecked()) {
                    soe="1";
                }
                b.putString("url", "http://tv.intern.de/rss/rss.php?site=Suche&q="+begriff+"&button1=suchen&ttl="+ttl+"&oe="+oe+"&soe="+soe+"&t="+zeitraum);
                i.putExtras(b);
                startActivity(i);
            }
        });
    }
}